package gui.screens;

import gui.CanvasManager;

import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Font;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;

/**
 * Class for the screen that is shown when program is exited.
 * @author Sandra Mayer
 *
 */
public class ExitScreen extends Canvas implements CommandListener {

	private static ExitScreen instance;
	
	private Command exitYesCmd;
	private Command exitNoCmd;
	private Image backgroundImg;
	
	/**
	 * Private Constructor.
	 *
	 */
	private ExitScreen() {
		exitYesCmd = new Command("Yes", Command.EXIT, 0);
		exitNoCmd = new Command("No", Command.CANCEL, 0);
		
		try {
			backgroundImg = Image.createImage("/mainWhite.jpg");
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		this.addCommand(exitYesCmd);
		this.addCommand(exitNoCmd);
		this.setCommandListener(this);
	}
	
	/**
	 * This method returns an instance of the exit screen.
	 * @return instance of the exit screen
	 */
	public static ExitScreen getInstance() {
		if (instance == null)
			instance = new ExitScreen();
		CanvasManager.setCurScreen(instance);
		return instance;
	}

	/**
	 * Does the command action.
	 */
	public void commandAction(Command cmd, Displayable dis) {
		if (cmd == exitYesCmd) {
			CanvasManager.destroyApp();
		} else if (cmd == exitNoCmd) {
			CanvasManager.showOldScreen();
		}
	}

	/**
	 * Paints the screen.
	 */
	protected void paint(Graphics g) {
		g.setColor(0xffffff);
		g.fillRect(0, 0, getWidth(), getHeight());
		g.translate((getWidth() - backgroundImg.getWidth()) / 2,
				(getHeight() - backgroundImg.getHeight()) / 2);

		g.drawImage(backgroundImg, 0, 0, Graphics.TOP | Graphics.LEFT);

		g.setFont(Font.getFont(Font.FACE_SYSTEM, Font.STYLE_BOLD,
				Font.SIZE_MEDIUM));
		g.setColor(0x000000);
		
		g.drawString("Are you", 95, 100, Graphics.LEFT | Graphics.TOP);
		g.drawString("sure?", 100, 125, Graphics.LEFT | Graphics.TOP);
		
	}
}
